/**
 * 根据参数名 获取 URL 路径中的参数
 * @param {String} name 要读取的参数名称
 */
class toolUtils {
  getUrlParam(name) {
    return (
      decodeURIComponent(
        (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(
          location.href
        ) || [, ''])[1].replace(/\+/g, '%20')
      ) || null
    )
  }

  // 校验手机号
  validator(val) {
    return /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(
      val
    )
  }
  // 深拷贝
  deepClone(obj) {
    return JSON.parse(JSON.stringify(obj))
  }

  callNativeMethod(methodName, params) {
    const u = navigator.userAgent
    const isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
    const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1
    if (isiOS) {
      if (JSON.stringify(params) === '{}' || !params) {
        return window.webkit.messageHandlers[methodName].postMessage()
      }
      if (typeof params === 'string') {
        return android[methodName](params)
      }
      return window.webkit.messageHandlers[methodName].postMessage(params)
    }
    if (isAndroid) {
      if (JSON.stringify(params) === '{}' || !params) {
        return android[methodName]()
      }
      if (typeof params === 'string') {
        return android[methodName](params)
      }
      return android[methodName](JSON.stringify(params))
    }
  }
  isMobile() {
    let flag = navigator.userAgent.match(
      /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
    )
    return flag
  }
  /**
   * 构造树型结构数据
   * @param {*} data 数据源
   * @param {*} id id字段 默认 'id'
   * @param {*} parentId 父节点字段 默认 'parentId'
   * @param {*} children 孩子节点字段 默认 'children'
   */
  handleTree(data, id, parentId, children) {
    let config = {
      id: id || 'id',
      parentId: parentId || 'parentId',
      childrenList: children || 'children',
    }

    var childrenListMap = {}
    var nodeIds = {}
    var tree = []

    for (let d of data) {
      let parentId = d[config.parentId]
      if (childrenListMap[parentId] == null) {
        childrenListMap[parentId] = []
      }
      nodeIds[d[config.id]] = d
      childrenListMap[parentId].push(d)
    }

    for (let d of data) {
      let parentId = d[config.parentId]
      if (nodeIds[parentId] == null) {
        tree.push(d)
      }
    }

    for (let t of tree) {
      adaptToChildrenList(t)
    }

    function adaptToChildrenList(o) {
      if (childrenListMap[o[config.id]] !== null) {
        o[config.childrenList] = childrenListMap[o[config.id]]
      }
      if (o[config.childrenList]) {
        for (let c of o[config.childrenList]) {
          adaptToChildrenList(c)
        }
      }
    }
    return tree
  }
  /**
   * 根据参数名 获取 URL 路径中的参数
   * @param {String} name 要读取的参数名称
   */
  getUrlParam(name) {
    return (
      decodeURIComponent(
        (new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(
          location.href
        ) || [, ''])[1].replace(/\+/g, '%20')
      ) || null
    )
    // let reg = new RegExp('(^|&)'+name+'=([^&]*)');
    // console.log(window.location.hash.substr(1),'window.location.search')
    // let r = window.location.hash.substr(1).match(reg);
    // if(r!=null)return decodeURIComponent(r[2]);
  }
  /**
   *微信授权页面
   *
   * @export
   * @param {*} appId
   * @param {*} url
   * @param {*} scope
   */
  goWechatScopeUrl(callBackUrl,appId, scope = 'snsapi_base') {
    let goUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${callBackUrl}&response_type=code&scope=${scope}&state=STATE#wechat_redirect`;
    return goUrl
  }
}

let utilTool = new toolUtils()
export default utilTool
